/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverClasses;

import commonClasses.Price;
import commonClasses.commonInterfaces.HostModelIntf;
import commonClasses.commonInterfaces.ItemIntf;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Pozytywny!
 */
public class HostModel implements HostModelIntf {

    private ArrayList<ItemIntf> _items = new ArrayList<ItemIntf>();
    private String _identifier = "";
    private ArrayList<String> userIdentifiers = new ArrayList<String>();

    public HostModel(String ident) {
        _identifier = ident;
    }

    public void addItem(ItemIntf item) {
        _items.add(item);
    }

    public ItemIntf getItem(long id) throws Exception {

        for (Iterator<ItemIntf> it = _items.iterator(); it.hasNext();) {
            ItemIntf itemIntf = it.next();
            if (itemIntf.getId() == id) {
                return itemIntf;
            }
        }
        throw new Exception("Item not found, given id = " + id);
    }

    public ArrayList<ItemIntf> getItems() {
        return _items;
    }

    public ArrayList<ItemIntf> getFinishedUserAuctions(String hash) {
        /* jeżeli hash znajduje się na liście bidujących dany przedmiot
         *
         *
         *
         */
        ArrayList<ItemIntf> itemList = new ArrayList<ItemIntf>();

        Iterator<ItemIntf> it = _items.iterator();
        while (it.hasNext()) {
            ItemIntf item = it.next();
            if (!item.isAuctionOnline() && item.isAmongAuctionUsers(hash)) {
                itemList.add(item);
            }
        }

        return itemList;
    }

    public String getHostIdentifier() {
        return _identifier;
    }

    public ArrayList<String> getRegisteredIdentifiers() {
        return userIdentifiers;
    }

    public void addIdentifier(String ident) {
        userIdentifiers.add(ident);
    }

    public boolean addUserToAuction(String hash, long itemID) {
        try {
            ItemIntf item = getItem(itemID);
            item.addUserIdentifier(hash);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


        return true;
    }

    public boolean processNewBid(String userHash, long itemID, Price price) {

        ItemIntf item = null;

        try {
            item = getItem(itemID);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        if (item == null) {
            return false;
        }

        if (item.isAmongAuctionUsers(userHash) && item.getPrice().getValue() < price.getValue()) {
            item.setItemPrice(price);
            return true;
        }

        return false;
    }

    public void finishAuction(long itemId) {
        for (int i = 0; i < _items.size(); i++) {
            if (_items.get(i).getId() == itemId) {
                _items.get(i).finishAuction();
                break;
            }
        }
    }
}
